package com.zh.harmony.ormlitesample.entity;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * @author wally
 * @date 2022/08/09
 * <p>
 * Article文章表数据库实体类
 * <p>
 * 注解：
 * DatabaseTable：通过其中的tableName属性指定数据库名称
 * DatabaseField：代表数据表中的一个字段
 * ForeignCollectionField：一对多关联，表示一个UserEntity关联着多个ArticleEntity（必须使用ForeignCollection集合）
 * <p>
 * 属性：
 * foreignColumnName：外键约束指向的类中的属性名
 * foreign：当前字段是否是外键
 * foreignAutoRefresh：如果这个属性设置为true，在关联查询的时候就不需要再调用refresh()方法了
 */
@DatabaseTable(tableName = "tb_article")
public class ArticleEntity {
    /**
     * 主键Id
     */
    @DatabaseField(generatedId = true, columnName = "id", unique = true)
    private Long id;
    /**
     * 标题
     */
    @DatabaseField(columnName = "title")
    private String title;
    /**
     * 内容
     */
    @DatabaseField(columnName = "content")
    private String content;
    /**
     * 关联用户表实体
     */
    @DatabaseField(
            columnName = "user_id",
            foreign = true,
            foreignAutoRefresh = true,
            foreignAutoCreate = true,
            foreignColumnName = UserEntity.USER_TABLE_COLUMN_NAME_ID)
    private UserEntity user;

    public ArticleEntity() {
    }

    public ArticleEntity(Long id, String title, String content, UserEntity user) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.user = user;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public UserEntity getUser() {
        return user;
    }

    public void setUser(UserEntity user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "ArticleEntity{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                ", user=" + user +
                '}';
    }
}